﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace ImportUtility
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Browsing..";
            fdlg.InitialDirectory = @"D:\ ";
            fdlg.Filter ="Text files (*.txt | .txt | All files (*.*) | *.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = fdlg.FileName;
            }

        }
        IImportUtility _importInterface;
        private void btnImport_Click(object sender, EventArgs e)
        {
            //BMJO : based on the selection of the device this section creates the 
            //proper class instnace.
            if (comboBox1.Text.ToLower() == "biofinger mini")
            {
                BioFingerUtility _importUtility = new BioFingerUtility();
                _importInterface = (IImportUtility)_importUtility;
            }
            //use only interface functions. and this is common code for all import
            if (_importInterface != null)
             {
                 _importInterface.UpdateImportStatus += new EventHandler(_importUtility_UpdateImportStatus);
                 _importInterface.Init();
                
                if (!_importInterface.SetFile(textBox1.Text.ToString()))
                    MessageBox.Show("File is not present");

                
                UpdateProgressBar();
                if (checkBox1.Checked == true)
                {
                    if (_importInterface.Import(true))
                    {
                       
                        //txtStatus.Text = "User Added successfully";
                        //progressBar1.Value = progressBar1.Maximum;
                    }
                }
                else
                {
                    if (_importInterface.Import(false))
                    {

                        //txtStatus.Text = "User Added successfully";
                        //progressBar1.Value = progressBar1.Maximum;
                    }
                }                
                       
            }
            //ImporttoDB();
        }
        delegate void UpdateImportStatus(object sender, EventArgs e);
        void _importUtility_UpdateImportStatus(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new UpdateImportStatus(_importUtility_UpdateImportStatus),new object[] { sender, e });
                return;
            }
            UpdateImportStatusEvent updateEvent = (UpdateImportStatusEvent) e;
            if (updateEvent.ImportStatus == UpdateStatus.ImportProgressing)
            {
                //BMJO use proper code to make sure it will start from zero when reach the limit.
                //also use messgaes and status to give proper messages to user. Use message box on errors
                txtStatus.Text = updateEvent.StatusMessage;
                progressBar1.Increment(1);
            }
            else if (updateEvent.ImportStatus == UpdateStatus.ImportFailed)
            {
                txtStatus.Text = "Failed : " + updateEvent.StatusMessage;
                progressBar1.Value = 0;
            }
            else if (updateEvent.ImportStatus == UpdateStatus.ImportCompleted)
            {
                txtStatus.Text = updateEvent.StatusMessage;
                progressBar1.Value = progressBar1.Maximum;
            }
        }
        

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void UpdateProgressBar()
        {
            if (progressBar1.Value < progressBar1.Maximum)
            {
                progressBar1.Value += 1;
            }
            else
            {
                progressBar1.Value = 0;
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

       
    }
}
